package com.djsearch.ui;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.json.JSONException;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;

import com.djsearch.R;
import com.djsearch.adapter.GroupShopListViewAdapter;
import com.djsearch.adapter.WeiboListViewAdapter;
import com.djsearch.bean.GroupBuyGoods;
import com.djsearch.bean.WeiboBean;
import com.djsearch.controller.AsyncHandler;
import com.djsearch.controller.IRequest;
import com.djsearch.controller.IResponse;
import com.djsearch.controller.JSONReq;
import com.djsearch.controller.JSONResp;
import com.djsearch.ui.GroupShopActivity.RequestParamStruct;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/**
 * 微博页面
 * @author chenyang2
 *
 */
public class WeiboActivity extends BaseActivity{
	
	private Context mContext;
	private Button backBtn;
	private ListView weiboListView;//微博列表
	private Handler mHandler;
	private List<WeiboBean> nextPageWeiboList;
	private List<WeiboBean> weiboList;
	private WeiboListViewAdapter weiboListViewAdapter;
	private int PAGE = 1;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.weibo_layout);
		mContext = this;
		nextPageWeiboList = new ArrayList<WeiboBean>();
		weiboList = new ArrayList<WeiboBean>();
		//得到屏幕的分辨率
		getDisPlayMetrics();
		//初始化主界面
		initView();
		//初始化主界面相关点击事件
		initOnClick();
		//
		initHandler();
		/**
		 * 请求数据
		 */
	}

	@Override
	void initHandler() {
		
	}

	@Override
	void initView() {
		// TODO Auto-generated method stub
		backBtn = (Button)findViewById(R.id.weiboBackBtn);
		weiboListView = (ListView)findViewById(R.id.weiboListView);
		weiboListViewAdapter = new WeiboListViewAdapter(mContext,weiboList);
		weiboListView.setAdapter(weiboListViewAdapter);
		requestWeiboData();
	}

	@Override
	void initOnClick() {
		// TODO Auto-generated method stub
		backBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(mContext, MainActivity.class, 2);
			}
		});
		
		/**
		 * 滑动到底部自动加载功能
		 */
		weiboListView.setOnScrollListener(new AbsListView.OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				if ( scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
					if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
						if (!(progressBarPopuWindow != null && progressBarPopuWindow.isShowing())) {
//					        RequestParamStruct.page = PAGE;
					        requestWeiboData();
						}
					}
				}
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
			}
		});
		
		weiboListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
//				startActivity(mContext, WeiboDetailsActivity.class, 0);
//				//去看看
//				Intent intent=new Intent();
//		        intent.setClass(mContext, WebViewActivity.class);
//		        intent.putExtra("url", weiboList.get(position).getUrl());
//		        bundle.putString("whichBuyType", "groupShop");
//		        startActivity(intent);
//		        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
		        
		        
		        
		      //去看看
				Intent intent=new Intent();
		        intent.setClass(mContext, WebViewActivity.class);
		        Bundle bundle = new Bundle();
		        bundle.putString("url", weiboList.get(position).getUrl());
		        bundle.putString("whichBuyType", "groupShop");
		        intent.putExtras(bundle);
//		        intent.putExtra("url", groupBuyGoods.getShopUrl());
		        startActivity(intent);
		        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
		        
			}
		});
	}

	
	private void requestWeiboData() {
		IRequest request = new JSONReq("weibo");
		request.putParam("page",String.valueOf(PAGE));
		request.putParam("pageNum", "10");
		HTTP.post(request, new AsyncHandler(){
			@Override
			public void onStart() {
				showDialog("正在加载...");
			}
			@Override
			public void onSuccess(JSONResp response) {
				// TODO Auto-generated method stub
				int result = response.result();
				if(IResponse.Success==result){
				    try {
				    	PAGE++;
						Toast.makeText(mContext, response.getString("message"), 5000).show();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					getDataToResolve(response);
				} else if (IResponse.Fail==result) {
					try {
						Toast.makeText(mContext, response.getString("message"), 5000).show();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
					missDialog();
			}
			@Override
			public void onFailure(Throwable e, String response) {
				// TODO Auto-generated method stub
				missDialog();
			}
			
		});
	
		
	
	}

	@Override
	void getDataToResolve(JSONResp response) {
		// TODO Auto-generated method stub

		try {
			Gson gson = new Gson();   
	        Type type = new TypeToken<List<WeiboBean>>(){}.getType();  //指定集合对象属性  
	        List<WeiboBean> fromJson = gson.fromJson(response.getString("returnMessage"), type); 
	        nextPageWeiboList.addAll(fromJson);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (nextPageWeiboList.size() == 0) {
				//当没有更多的数据了处理
			} else {
				weiboList.addAll(nextPageWeiboList);
				nextPageWeiboList.clear();
			}
			weiboListViewAdapter.refreshData(weiboList);
		}
	}

	
	
	
}
